require 'jcode'
class SubHotPost < ActiveRecord::Base
  
  belongs_to :hot_post

  validates_uniqueness_of :hot_post_id,
    :scope => [:page_num, :position]
  
  validates_presence_of :hot_post_id,
    :content,
    :released_at,
    :url,
    :author,
    :page_num,
    :position,
    :words_count

  def before_save
    self.words_count ||= content.jlength
  end

  def after_create
    hot_post.the_last_sub_hot_post = self
    hot_post.the_last_lz_sub_hot_post = self if hot_post.author == author
    hot_post.save(false)
#    puts 'the_last_lz_sub_hot_post saved'
  end
  
  def created_by_lz?
    author == hot_post.author
  end
  
  def pre_neighbor_of_lz
    SubHotPost.first(
      :conditions => [
        'hot_post_id = ? and  id < ? and author = ?',
        hot_post.id,
        id,
        hot_post.author
      ],
      :order => 'id desc')  
  end
  
  def pre_neighbor_of_no_lz
    SubHotPost.first(
      :conditions => [
        'hot_post_id = ? and  id < ? and author != ?',
        hot_post.id,
        id,
        hot_post.author
      ],
      :order => 'id desc')
  end
end
